package com.hxz.lesson01;

import java.sql.*;

public class JdbcFirstDemo {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //1、加载驱动
        Class.forName("com.mysql.jdbc.Driver"); // 固定写法，加载驱动


        //2、用户信息和url
        String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String password = "aaaa";

        //3、连接成功，返回数据库对象
        Connection connection = DriverManager.getConnection(url, username, password);

        //4、执行sql得对象
        Statement statement = connection.createStatement();

        //5、执行sql得对象  去  执行sql，可能存在结果，查看返回结果
        String sql = "select * from users";
        ResultSet resultSet = statement.executeQuery(sql); // 返回得结果集

        while (resultSet.next()) {
            System.out.println("id=" + resultSet.getObject("id"));
            System.out.println("name=" + resultSet.getObject("NAME"));
            System.out.println("pwd=" + resultSet.getObject("PASSWORD"));
            System.out.println("email=" + resultSet.getObject("email"));
            System.out.println("birthday=" + resultSet.getObject("birthday"));
        }

        //6、释放连接
        resultSet.close();
        statement.close();
        connection.close();

    }
}
